<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\CompanyHope as CompanyHopeModel;
use app\common\model\Member as MemberModel;
use app\common\model\Practice as PracticeModel;
use app\common\model\CompanyShan as CompanyShanModel;
use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;

/**
 * 发愿控制器
 * Class Companyhope
 * @package app\home\controller
 */
class Companyhope extends Base
{
    /**
     * [index  明细列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyHopeModel;
        $mmodel = new MemberModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try{
            if(empty($param['home'])){
                $map['mid'] = $user['id'];
            }

            $map['comid'] = $param['comid'];
            $field = 'id,status,reply,content,create_time,mid,update_time';
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size,$field);
            foreach($lists as &$v){
                $member = $mmodel->findData($v['mid'],'id','img,username');
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $member['img'] = config('domain_name').$member['img'];
                }
                $v['member'] = $member;
                $v['addtime'] = date('Y-m-d',strtotime($v['create_time']));
            }
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  发愿]
     * @param
     * @author [C。]
     * @return json
     */
    public function add()
    {
        $param = input('param.');
        $model = new CompanyHopeModel;
        $pmodel = new PracticeModel;
        $user = $this->user;
        try{
            $param['mid'] = $user['id'];
            $model->addData($param);
            $practice = $pmodel->getRound(['type'=>3],'content');
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '发愿失败'.$e->getMessage());
        }
        return admin_json(1, '发愿成功',$practice['content']);
    }
    /**
     * [donornum  捐款人数]
     * @param
     * @author [C。]
     * @return json
     */
    public function donornum()
    {
        $param = input('param.');
        $model = new CompanyShanModel;
        $cmodel = new CompanyGongdengOrderModel;
        try{
            $data = $model->findData($param['id'],'comid','donor_num');
            if(empty($data)){
                $data['donor_num'] = 0;
            }
            $data['really'] = $cmodel->getAllCount(['type'=>2,'status'=>1,'comid'=>$param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [kaishi  开示]
     * @param
     * @author [C。]
     * @return json
     */
    public function kaishi()
    {
        $param = input('param.');
        $model = new CompanyHopeModel;
        $data = [];
        try{
            $data['id'] = $param['id'];
            $data['status'] = 1;
            $data['reply'] = $param['reply'];
            $model->editData($data);
        }catch (\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '失败' . $e->getMessage());
        }
        return admin_json(1, '成功');
    }
}